package com.longtop.framework.sys.auth.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.common.CacheUpdate;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.auth.model.TSysAuthOper;

public class AuthOperService extends BaseService {

	public static final String BEAN_ID = "authOperService";

	/**
	 * 根据角色代码取得该角色的权限
	 * 
	 * @param roleId
	 * @return
	 */
	public List getAuthOperListByRoleId(String roleId) {

		return super.queryAllByCondition("from TSysAuthOper", new String[] { "roleId" }, new String[] { QueryConditions.EQUAL }, new Object[] { roleId });
	}

	/**
	 * 批量更新操作权限
	 * 
	 * @param list
	 *            要更新的权限列表
	 * @param roleId
	 *            要更新的角色代码
	 * @param sessionObject
	 *            SessionObject对象
	 * @return 更新成功的记录数
	 */
	public Integer[] saveAuthOper(List list, String roleId, SessionObject sessionObject) {
		int insertCount = 0;
		int deleteCount = 0;
		TSysAuthOper bean;
		TSysAuthOper po;

		// 保存用户菜单或权限之前清空用户已经存在的菜单或权限
		List tmpList = super.queryAllByCondition("from TSysAuthOper", new String[] { "roleId" }, new String[] { QueryConditions.EQUAL }, new Object[] { roleId });

		Iterator tmpIt = tmpList.iterator();
		while (tmpIt.hasNext()) {
			TSysAuthOper tmp = (TSysAuthOper) tmpIt.next();

			// 删除数据
			this.delete(tmp);
			deleteCount++;
		}

		// 插入权限
		Iterator it = list.iterator();
		while (it.hasNext()) {
			bean = (TSysAuthOper) it.next();
			po = new TSysAuthOper();
			po.setMenuOrFuncId(bean.getMenuOrFuncId());
			po.setAuthType(bean.getAuthType());
			po.setRoleId(roleId);

			// 保存数据
			super.save(po);
			insertCount++;
		}

		// 更新缓存：
		this.getHibernateTemplate().flush();
		CacheUpdate.getCacheUpdate().updateAuthOperMenuMap();
		CacheUpdate.getCacheUpdate().updateAuthOperFuncMap();

		return new Integer[] { new Integer(insertCount), new Integer(0), new Integer(deleteCount) };
	}

	/**
	 * 读取 菜单权限authDate表 ,并根据 角色id roleId 组织成一个 Map， 此方法供 系统启动时候的Listener调用。
	 * 
	 * @param roleId
	 *            角色id， 如果为空，则取全部数据
	 * @param operType
	 *            权限类型， 1为菜单权限， 2为功能权限
	 * @return 菜单权限的Map
	 * @author 胡砥峰
	 */
	public Map getAuthOperMap(String roleId, String operType) {

		Map TSysAuthOperMap = new HashMap();

		// ibatis查询条件
		Map hashMap = new HashMap();
		hashMap.put("operType", operType);

		if (StringUtils.isNotEmpty(roleId)) {
			hashMap.put("roleId", roleId);
		}

		List list = this.queryForListByIbatis("sysAuthOperSql.selectAuthOperList", hashMap);
		List<String> subList = new ArrayList();
		TSysAuthOper tbXtTSysAuthOper = null;
		String tmpRoleId = null;
		boolean isFirst = true;
		Iterator it = list.iterator();
		while (it.hasNext()) {
			tbXtTSysAuthOper = (TSysAuthOper) it.next();
			if (isFirst) {
				tmpRoleId = tbXtTSysAuthOper.getRoleId();
				isFirst = false;
			}
			if (!tmpRoleId.equals(tbXtTSysAuthOper.getRoleId())) {
				TSysAuthOperMap.put(tmpRoleId, subList);
				subList = new ArrayList();
			}
			subList.add(tbXtTSysAuthOper.getMenuOrFuncId());
			tmpRoleId = tbXtTSysAuthOper.getRoleId();
		}
		if (tmpRoleId != null && subList.size() > 0) {
			TSysAuthOperMap.put(tmpRoleId, subList);
		}

		return TSysAuthOperMap;
	}

}